/**
 * 
 */

$(document).ready(function() {
	getDB();
	$("#submit").click(function() {
		insert();
	});
});

function insert() {
	if ($("#inputText").val() == null || $("#inputText").val() == "") {
		alert($("#inputText").val());
		return;
	}
	$.ajax({
		type : "POST",
		url : "write",
		async : false,
		data : {
			"a" : "add",
			"content" : $("#inputText").val()
		},
		success : function() {
			// alert("insert");
		},
		error : function(xhr, status, error) {
			alert("ERROR");
		}
	});
}

function getDB() {
	$.ajax({
		type : "POST",
		url : "list",
		async : false,
		contentType : "application/x-www-form-urlencoded; charset=UTF-8",
		data : {
			"a" : "list"
		},
		dataType : "JSON",
		success : makeTable,
		error : function(xhr, status, error) {
			alert("xhr : " + xhr + "\nstatus : " + status + "\nerror : "
					+ error);
		}
	});
}

function makeTable(json) {
	$("#contentTable *").remove();

	var list = json.AnnoymousSNS;

	var row_count = list.length;

	// alert(row_count);

	for (var i = 0; i < row_count; i++) {

		var content = list[i].content;
		var time = list[i].time;

		var html = "<tr>";
		html += "	<td>[" + time + "]</td>";
		html += "</tr>";
		html += "<tr>";
		html += "	<td>" + content + "</td>";
		html += "</tr>";

		$("#contentTable").append(html);
	}

}